package com.youlai.system.controller;

import com.youlai.system.common.request.LayoutSaveRequest;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.LayoutConfig;
import com.youlai.system.service.LayoutConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "911. Layout Controller")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/layout")
public class LayoutConfigController {



    private final LayoutConfigService layoutConfigService;
    /**
     * 保存布局配置
     */
    @PostMapping("/save")
    public Result<LayoutConfig> saveLayout(@RequestBody LayoutSaveRequest request) {
        try {
            LayoutConfig layout = layoutConfigService.save(request);
            return Result.success(layout);
        } catch (Exception e) {
            return Result.failed("保存布局失败: " + e.getMessage());
        }
    }

    /**
     * 获取布局配置列表
     */
    @GetMapping("/list")
    public Result<List<LayoutConfig>> getLayoutList(
            @RequestParam(required = false) String pageName,
            @RequestParam(required = false) String layoutName,
            @RequestParam(required = false) Boolean isDefault) {
        try {
            List<LayoutConfig> layouts = layoutConfigService.getLayoutList(pageName, layoutName, isDefault);
            return Result.success(layouts);
        } catch (Exception e) {
            return Result.failed("获取布局列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据页面名称获取默认布局
     */
    @GetMapping("/default/{pageName}")
    public Result<LayoutConfig> getDefaultLayout(@PathVariable String pageName) {
        try {
            LayoutConfig layout = layoutConfigService.getDefaultLayoutByPage(pageName);
            return Result.success(layout);
        } catch (Exception e) {
            return Result.failed("获取默认布局失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取布局配置
     */
    @GetMapping("/{id}")
    public Result<LayoutConfig> getLayoutById(@PathVariable Long id) {
        try {
            LayoutConfig layout = layoutConfigService.getLayoutById(id);
            return Result.success(layout);
        } catch (Exception e) {
            return Result.failed("获取布局失败: " + e.getMessage());
        }
    }

    /**
     * 更新布局配置
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateLayout(@PathVariable Long id, @RequestBody LayoutSaveRequest request) {
        try {
            boolean success = layoutConfigService.updateLayout(id, request);
            return Result.success(success);
        } catch (Exception e) {
            return Result.failed("更新布局失败: " + e.getMessage());
        }
    }

    /**
     * 删除布局配置
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteLayout(@PathVariable Long id) {
        try {
            boolean success = layoutConfigService.deleteLayout(id);
            return Result.success(success);
        } catch (Exception e) {
            return Result.failed("删除布局失败: " + e.getMessage());
        }
    }

    /**
     * 设置默认布局
     */
    @PutMapping("/{id}/default")
    public Result<Boolean> setDefaultLayout(@PathVariable Long id) {
        try {
            boolean success = layoutConfigService.setDefaultLayout(id);
            return Result.success(success);
        } catch (Exception e) {
            return Result.failed("设置默认布局失败: " + e.getMessage());
        }
    }

}